package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Collect;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.mapper.CollectMapper;
import cn.wolfcode.trip.base.mapper.StrategyMapper;
import cn.wolfcode.trip.base.query.CollecQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Copyright: Copyright (c) 2019 Super520
 * 描述：大攻略业务层
 *
 * @package: cn.wolfcode.trip.base.service.impl
 * @author: Patch
 * @date: 2019/5/23 22:59
 * 总结:
 */
@Service
public class StrategyServiceImplImpl implements IStrategyService {
    @Autowired
    private StrategyMapper strategyMapper;
    @Autowired
    private CollectMapper collectMapper;

    @Override
    public PageInfo<Strategy> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(),qo.getOrderBy());
        List<Strategy> list = strategyMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void saveOrUpdate(Strategy strategy) {
        if (strategy.getId() != null) {
            strategyMapper.updateByPrimaryKey(strategy);
        } else {
            strategyMapper.insert(strategy);
        }
    }

    @Override
    public Strategy get(Long id) {
        return strategyMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Strategy> listAll(Integer state) {
        return strategyMapper.selectAll(state);
    }

    @Override
    public Integer getUserComment(Long userId) {
        return strategyMapper.getUserComment(userId);
    }

    @Override
    public PageInfo<Strategy> selectCollectStrategy(Collect collect, CollecQueryObject qo) {
        collect.setUser(UserContext.getCurrentUser());
        List<Integer> integers = collectMapper.selectCollectId(collect);
       List<Strategy> strategies =  strategyMapper.selectByCollectId(integers);
        return new PageInfo<>(strategies);
    }

}
